best time to buy and sell stock python

25

def maxProfit(self, prices: List[int]) -> int:
    n = len(prices)

    if n == 1:
        return 0

    min_price = prices[0]
    max_profit = -float('inf')

    for price in prices:
        min_price = min(min_price, price)
        max_profit = max(max_profit, price - min_price)

    return max_profit

Comments

Submit
0 Comments